/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-01-20     xieya       the first version
 */
#include  "bsp_timer.h"

extern uint32_t flow_meter1_calc;

/* INT_SRC_TMRA_4_CMP Callback. */
static void INT_SRC_TMRA_4_CMP_IrqCallback(void)
{
    //add your codes here
    flow_meter1_calc++;
}


void BSP_TIMERA_GPIO_Config(void)
{
    stc_gpio_init_t stcGpioInit;


    GPIO_SetFunc(GPIO_PORT_H,GPIO_PIN_02,GPIO_FUNC_4);//TIMA-4-PWM7

    stc_irq_signin_config_t stcIrq;

    stcIrq.enIntSrc = INT_SRC_TMRA_4_CMP;
    stcIrq.enIRQn = INT081_IRQn;
    stcIrq.pfnCallback = &INT_SRC_TMRA_4_CMP_IrqCallback;
    (void)INTC_IrqSignIn(&stcIrq);
    NVIC_ClearPendingIRQ(INT081_IRQn);
    NVIC_SetPriority(INT081_IRQn, DDL_IRQ_PRIO_15);
    NVIC_EnableIRQ(INT081_IRQn);
}


//TimerA Config
void BSP_TimerA_Cfg(void)
{
    stc_tmra_init_t stcTmraInit;

    /* Enable TMRA_4 peripheral clock */
    FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMRA_4, ENABLE);


    /************************* Configure TMRA_4 counter *************************/
    (void)TMRA_StructInit(&stcTmraInit);
    /* Config software count */
    stcTmraInit.sw_count.u16ClockDiv = TMRA_CLK_DIV1;
    stcTmraInit.sw_count.u16CountMode = TMRA_MD_SAWTOOTH;
    stcTmraInit.sw_count.u16CountDir = TMRA_DIR_UP;
    stcTmraInit.u32PeriodValue = 0xFFFFU;
    (void)TMRA_Init(CM_TMRA_4, &stcTmraInit);

    /************************* Configure TMRA_4_4 CMP ***************************/
    /* CAP pin function set */
    TMRA_SetFunc(CM_TMRA_4, TMRA_CH7, TMRA_FUNC_CAPT);
    /* Config hardware capture */
    TMRA_HWCaptureCondCmd(CM_TMRA_4, TMRA_CH7, TMRA_CAPT_COND_PWM_FALLING, ENABLE);
    /* Enable CMP interrupt */
    TMRA_IntCmd(CM_TMRA_4, TMRA_INT_CMP_CH7, ENABLE);

    /* Config hardware start */
    TMRA_HWStartCondCmd(CM_TMRA_4, TMRA_START_COND_TRIG_FALLING, ENABLE);

    /* Start timerA */
    //TMRA_Start(CM_TMRA_4);

}


